package org.example.controller;
import org.example.pojo.Appointment;
import org.example.pojo.Doctor.OutpatientClinic;
import org.example.pojo.Doctor.Schedule;
import org.example.pojo.Result;
import org.example.service.AdminUserService;
import org.example.service.AppointmentService;
import org.example.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private HospitalService hospitalService;
    // 添加挂号
    @PostMapping("/addAppointment")
    public Result addAppointment(@RequestBody Appointment appointment) {
        List<Schedule> schedules = adminUserService.getDoctorScheduleByDate(appointment.getAppointmentTime().toString());
        boolean found = false;
        for (Schedule schedule : schedules) {
            if (schedule.getDoctorId().equals(appointment.getDoctorId())) {
                found = true;
                break;
            }
        }
        if (!found) {
            return Result.error("医生今天未排班");
        }
        if (appointment.getAppointmentTime() == null) {
            return Result.error("预约时间不能为空");
        }
        //通过医生获取得到科室id
        //通过医生id获取医生姓名
        String doctorName = adminUserService.getDoctorById(appointment.getDoctorId()).getName();
        appointment.setDoctorName(doctorName);
        Integer clinicId = adminUserService.getDoctorById(appointment.getDoctorId()).getClinicId();
        appointment.setClinicId(clinicId);
        //通过科室id获取科室名称
        List<OutpatientClinic> clinics = hospitalService.getAllClinics();
        clinics.forEach(clinic -> {
            if (clinic.getClinicId().equals(clinicId)) {
                appointment.setClinicName(clinic.getClinicName());
            }
        });
        appointmentService.addAppointment(appointment);
        return Result.success();
    }

    // 更新挂号状态
    @PutMapping("/updateAppointment")
    public Result updateAppointmentStatus(@RequestBody Appointment appointment) {
        appointmentService.updateAppointmentStatus(appointment);
        return Result.success();
    }
    //根据日期获取挂号列表
    @GetMapping("/getbyday")
    public Result getAppointmentByDay(){
        List<Appointment> appointments = appointmentService.getAppointmentByDay();
        appointments.forEach(appointment -> {
            //通过医生id获取医生姓名
            String doctorName = adminUserService.getDoctorById(appointment.getDoctorId()).getName();
            appointment.setDoctorName(doctorName);
            //通过科室id获取科室名称
            List<OutpatientClinic> clinics = hospitalService.getAllClinics();
            clinics.forEach(clinic -> {
                if (clinic.getClinicId().equals(appointment.getClinicId())) {
                    appointment.setClinicName(clinic.getClinicName());
                }
            });
            //根据patientId获取患者名字
            String patientName = adminUserService.getPatientById(appointment.getPatientId()).getName();
            appointment.setPatientName(patientName);
        });
        return Result.success(appointments);
    }

    // 获取挂号列表
    @GetMapping
    public Result getAppointmentList() {
        List<Appointment> appointments = appointmentService.getAppointmentList();
        appointments.forEach(appointment -> {
            //通过医生id获取医生姓名
            String doctorName = adminUserService.getDoctorById(appointment.getDoctorId()).getName();
            appointment.setDoctorName(doctorName);
            //通过科室id获取科室名称
            List<OutpatientClinic> clinics = hospitalService.getAllClinics();
            clinics.forEach(clinic -> {
                if (clinic.getClinicId().equals(appointment.getClinicId())) {
                    appointment.setClinicName(clinic.getClinicName());
                }
            });
            //根据patientId获取患者名字
            String patientName = adminUserService.getPatientById(appointment.getPatientId()).getName();
            appointment.setPatientName(patientName);
        });
        return Result.success(appointments);
    }
    //根据id获取挂号信息
    @GetMapping("/getAppointment")
    public Result getAppointmentById(Integer patientId) {
        List<Appointment> appointments = appointmentService.getAppointmentById(patientId);
        appointments.forEach(appointment -> {
            //通过医生id获取医生姓名
            String doctorName = adminUserService.getDoctorById(appointment.getDoctorId()).getName();
            appointment.setDoctorName(doctorName);
            //通过科室id获取科室名称
            List<OutpatientClinic> clinics = hospitalService.getAllClinics();
            clinics.forEach(clinic -> {
                if (clinic.getClinicId().equals(appointment.getClinicId())) {
                    appointment.setClinicName(clinic.getClinicName());
                }
            });
            //根据patientId获取患者名字
            String patientName = adminUserService.getPatientById(appointment.getPatientId()).getName();
            appointment.setPatientName(patientName);
        });
        return Result.success(appointments);
    }
    //删除挂号信息
    @DeleteMapping("/deleteAppointment")
    public Result deleteAppointment(Integer appointmentId) {
        appointmentService.deleteAppointment(appointmentId);
        return Result.success();
    }
}